package problem21;

//21.合并两个有序链表
//https://leetcode.cn/problems/merge-two-sorted-lists/description/


class Solution2 {
    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        ListNode head = new ListNode();
        ListNode cur1 = list1;
        ListNode cur2 = list2;
        ListNode curH = head;
        while(cur1 != null && cur2 != null) {
            if(cur1.val < cur2.val) {
                curH.next = cur1;
                cur1 = cur1.next;
            }else {
                curH.next = cur2;
                cur2 = cur2.next;
            }
            curH = curH.next;
        }
        if(cur1 != null) {
            curH.next = cur1;
        }
        if(cur2 != null) {
            curH.next = cur2;
        }
        return head.next;
    }
}